package com.example.spider.task.limit;

import cn.hutool.core.map.MapUtil;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.util.Map;

/**
 *
 * @author lym
 */
public class LimitStrategyJsonDeserializer extends JsonDeserializer<LimitStrategy> {

    @Override
    public LimitStrategy deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
        Map<String, Object> m = jsonParser.readValueAs(Map.class);
        if (MapUtil.isEmpty(m)) {
            return NoLimit.getInstance();
        }
        LimitStrategyEnum strategyEnum = LimitStrategyEnum.valueOfType((String) m.get(LimitStrategyJsonSerializer.TYPE));
        return strategyEnum.getParseFunction().apply(m);
    }
}
